package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.cartoon.CartoonBeanListView;
import com.taptech.doufu.bean.cartoon.CartoonListBean;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.ui.view.topicview.adapter.CartoonGridAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class CartoonGridImageView extends LinearLayout{
    private TextView tvTitle;
    private List<CartoonBeanListView> list;
    private RecyclerView listView;
    public CartoonGridImageView(Context context) {
        super(context);
    }

    public CartoonGridImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CartoonGridImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private View root_layout;
    private View linearLayout0;
    private Context context;
    private LinearLayout llMore;
    private CartoonGridAdapter contentAdapter;
    private String moreUrl;

    public CartoonGridImageView(final Context context, final CartoonListBean model){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.cartoon_grid_view, null);
        this.context = context;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        listView = (RecyclerView) view.findViewById(R.id.listView);
        llMore = (LinearLayout) view.findViewById(R.id.llMore);
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(moreUrl!=null && moreUrl.contains("/weex/")){
                    SimpleWeexActivity.startActivity(context,moreUrl);
                }else{
                    CartoonServices.clickMore(context,moreUrl,model.getTitle());
                }
            }
        });
        tvTitle.setText(model.getTitle());
        changeDayModelBg();
        if (list != null) {
            list.clear();
        }
        moreUrl = model.getMoreUrl();
        list = model.getList();
        if (contentAdapter != null) {
            contentAdapter.refrush(list);
        }else{
            contentAdapter = new CartoonGridAdapter(getContext(), list);
            MeasuredGridLayoutManager gridLayoutManager = new MeasuredGridLayoutManager(getContext(),3);
            gridLayoutManager.setScrollEnabled(false);
            listView.setLayoutManager(gridLayoutManager);
            listView.setAdapter(contentAdapter);
        }
        addView(view);
    }


    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_1));

    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));

    }

}
